package vip.ola.pay.channel.code;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import vip.ola.core.common.constant.PayConstant;
import vip.ola.core.common.util.MyLog;
import vip.ola.core.common.util.PayUtil;
import vip.ola.core.common.util.RedisUtil;
import vip.ola.core.entity.PayOrder;
import vip.ola.core.entity.code.CodeOrder;
import vip.ola.pay.channel.BasePayment;

import java.math.BigDecimal;

import static vip.ola.core.common.constant.CacheKey.ORDER_EXPIRE_TIME;
import static vip.ola.core.common.constant.CacheKey.ORDER_PAY;
import static vip.ola.core.common.constant.Constant.*;
import static vip.ola.core.common.constant.PayConstant.*;
import static vip.ola.core.common.constant.PayEnum.ERR_0010;
import static vip.ola.core.common.constant.PayEnum.ERR_0112;

/**
 * @author: dingzhiwei
 * @date: 17/12/24
 * @description:
 */
@Service
public class CodePaymentService extends BasePayment {

    private static final MyLog _log = MyLog.getLog(CodePaymentService.class);

    @Value("${config.payUrl}")
    protected String payUrlDomain;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public String getChannelName() {
        return PayConstant.CHANNEL_NAME_CODE;
    }

    @Override
    public JSONObject pay(PayOrder payOrder) {
        try {
            int codeType = 1;
            String ifCode = payOrder.getChannelId();
            boolean needThirdId = false;
            if (ifCode.startsWith(IF_CODE_ALIPAY_FLAG)) {
                if (IF_CODE_ALIPAY_QR_H5_ONE.equals(ifCode)) {
                    needThirdId = true;
                }
            } else if (ifCode.startsWith(IF_CODE_WECHAT_FLAG)) {
                codeType = 2;
            } else if (ifCode.startsWith(IF_CODE_JUHE_FLAG)) {
                codeType = 3;
            }

            JSONObject retObj = rpcCommonService.codeOrderService.distributeCodeOrder(payOrder, Long.valueOf(payOrder.getChannelMchId()), codeType, needThirdId);
            if (retObj.get(RETURN_PARAM_RETCODE).equals(RETURN_VALUE_SUCCESS)) {
                redisUtil.set(ORDER_PAY + payOrder.getPayOrderId(), retObj, ORDER_EXPIRE_TIME);
                String payUrl = payUrlDomain + "/code/pay_" + ifCode + ".htm?orderNo=" + payOrder.getPayOrderId();
                return PayUtil.makeRetPayUrl(payUrl, null);
            } else {
                int result = rpcCommonService.rpcPayOrderService.updateStatus4Fail(payOrder.getPayOrderId(), PAY_STATUS_INIT,
                        retObj.getString(RESULT_PARAM_ERRCODE), retObj.getString(RESULT_PARAM_ERRDES));
                _log.info("更新订单状态为支付失败: result={}", result);
            }
            return retObj;
        } catch (Exception e) {
            _log.error("分配二维码订单错误.", e);
            return PayUtil.makeRetFail(ERR_0010);
        }
    }

    @Override
    public JSONObject query(PayOrder payOrder) {
        try {
            CodeOrder codeOrder = rpcCommonService.codeOrderService.getByOrderNo(payOrder.getPayOrderId());
            if (codeOrder == null) {
                return PayUtil.makeRetFail(ERR_0112);
            }
            JSONObject result = new JSONObject();
            result.put("orderNo", codeOrder.getOrderNo());
            result.put("status", codeOrder.getStatus());
            result.put("amount", codeOrder.getAmount());
            result.put("paySuccTime", codeOrder.getConfirmTime());
            return result;
        } catch (Exception e) {
            return PayUtil.makeRetFail(ERR_0010);
        }

    }

    @Override
    public JSONObject close(PayOrder payOrder) {
        return null;
    }

}
